﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dude.Data.Meta;
using Dude.MLS;
using System.Data.Common;

namespace Dude.Data.Forms
{
    public partial class DatabaseConnectionInfoForm : Form
    {
        public DatabaseConnectionInfoForm()
        {
            InitializeComponent();

            Lang.LocalizeUI(this);

            edtProvider.Items.Clear();
            foreach (DataRow row in System.Data.Common.DbProviderFactories.GetFactoryClasses().Rows)
            {
                edtProvider.Items.Add(row[2].ToString());
            }

            //foreach (var col in System.Data.Common.DbProviderFactories.GetFactoryClasses().Columns)
            //{
            //    edtProvider.Items.Add(col.ToString());
            //}

        }

        DatabaseConnectionInfo _databaseConnectionInfo;
        public DatabaseConnectionInfo DatabaseConnectionInfo
        {
            get
            {
                return _databaseConnectionInfo;
            }
            set
            {
                _databaseConnectionInfo = value;
                LoadDatabaseConnectionInfo();
            }
        }

        private DbConnectionStringBuilder _connStrBuilder;
        public DbConnectionStringBuilder ConnectionStringBuilder
        {
            get
            {
                return _connStrBuilder;
            }
            set
            {
                _connStrBuilder = value;
                if (_connStrBuilder != null)
                {
                    try
                    {
                        _connStrBuilder.ConnectionString = edtConnString.Text;
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show("Exception: {0}".GetText(exception.ToString()));
                    }
                }
                connPropertyEditor.SelectedObject = _connStrBuilder;
            }
        }

        /// <summary>
        /// Read DatabaseConnectionInfo property and fills controls
        /// </summary>
        public void LoadDatabaseConnectionInfo()
        {
            edtConnectionName.Text = DatabaseConnectionInfo.ConnectionName;
            edtAliases.Lines = DatabaseConnectionInfo.Aliases;
            edtProvider.Text = DatabaseConnectionInfo.ProviderInvariantName;
            edtAssemblyName.Text = DatabaseConnectionInfo.ProviderAssembly;
            edtConnString.Text = DatabaseConnectionInfo.ConnectionString;
        }

        /// <summary>
        /// Store configuration to DatabaseConnectionInfo property
        /// </summary>
        public void CommitDatabaseConnectionInfo()
        {
            DatabaseConnectionInfo.ConnectionName = edtConnectionName.Text;
            DatabaseConnectionInfo.Aliases = edtAliases.Lines;
            DatabaseConnectionInfo.ProviderInvariantName = edtProvider.Text;
            DatabaseConnectionInfo.ProviderAssembly = edtAssemblyName.Text;
            DatabaseConnectionInfo.ConnectionString = edtConnString.Text;
        }

        private void LoadProvider_Click(object sender, EventArgs e)
        {
            try
            {
                System.Data.Common.DbProviderFactory factory = Dude.Data.Providers.GetDbProviderFactory(edtProvider.Text, edtAssemblyName.Text);

                ConnectionStringBuilder = factory.CreateConnectionStringBuilder();
            }
            catch(Exception exception)
            {
                MessageBox.Show("Exception: {0}".GetText(exception.ToString()));
            }
        }

        private void connPropertyEditor_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            edtConnString.Text = ConnectionStringBuilder.ToString();

            // for some buggy wroten ConnectionStringBuilders (i.e. Npgsql)
            connPropertyEditor.Refresh(); 
        }

        private void btnRevert_Click(object sender, EventArgs e)
        {
            LoadDatabaseConnectionInfo();
            ConnectionStringBuilder = null;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            CommitDatabaseConnectionInfo();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ConnectionStringBuilder = null;
        }

        private void InvalidateConnPropertyEditor(object sender, EventArgs e)
        {
            ConnectionStringBuilder = null;
        }
    }
}
